/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.AR;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

public class ARPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {


	public static final String KEY_HEIGHT				= "height";
	public static final String KEY_NOT_HEIGHT			= "noHeight";
	public static final String KEY_FLOOR				= "floor";
	public static final String KEY_USE_FLOOR			= "useFloor";
	public static final String KEY_GPS					= "useGps";
	public static final String KEY_IS_DIST_FILTER		= "useDistFilter";
	public static final String KEY_DIST_FILTER			= "distFilter";
//	public static final String KEY_MOVEMENT_LINE		= "moveLine";
	public static final String KEY_MEASURES				= "showMeasures";
//	public static final String KEY_LABELS				= "showLabels";
	public static final String KEY_MOVE_LABELS			= "moveLabels";
//	public static final String KEY_COMPASS_CORRECTION	= "compassCorrection";
//	public static final String KEY_CORRECTION_SIGNUM	= "compassCorrectionSignum";
	
	
	EditTextPreference userTestPref;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	    setPreferenceScreen(createPreferenceHierarchy());
	      
	    // Set up a listener whenever a key changes            
	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	    
	    blockVisibility();
	}

	private PreferenceScreen createPreferenceHierarchy() {
	    // Root
	    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
	    
	    /* Height Main */
	    PreferenceCategory dialogBasedPrefCat7 = new PreferenceCategory(this);
	    dialogBasedPrefCat7.setTitle("Altitude");
	    root.addPreference(dialogBasedPrefCat7);
	    
		 // GPS height active
	    CheckBoxPreference heightPref = new CheckBoxPreference(this);
	    heightPref.setKey(KEY_NOT_HEIGHT);
	    heightPref.setTitle("Avoid altitude info");
	    heightPref.setSummary("Application won't use altitude info");
	    dialogBasedPrefCat7.addPreference(heightPref);
	    
	    /* Height Properties */
	    PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
	    dialogBasedPrefCat.setTitle("Altitude properties");
	    root.addPreference(dialogBasedPrefCat);
	    
	    // GPS height active
	    CheckBoxPreference gpsHeightPref = new CheckBoxPreference(this);
	    gpsHeightPref.setKey(KEY_GPS);
	    gpsHeightPref.setTitle("Active GPS Altitude");
	    gpsHeightPref.setSummary("Take altitude data from GPS");
	    dialogBasedPrefCat.addPreference(gpsHeightPref);

	    
	    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
	    
		// User height 
		String height = sharedPreferences.getString(KEY_HEIGHT, null); 
		 
		if (height == null)
			height = "1.75";
			     
		EditTextPreference userHeightTestPref2 = new EditTextPreference(this);
        userHeightTestPref2.setDialogTitle("User altitude (meters)");
        userHeightTestPref2.setKey(KEY_HEIGHT);
        userHeightTestPref2.setTitle("Altitude");
        userHeightTestPref2.setSummary(height);
        dialogBasedPrefCat.addPreference(userHeightTestPref2);
        
        
        CheckBoxPreference useFloorPref = new CheckBoxPreference(this);
        useFloorPref.setKey(KEY_USE_FLOOR);
        useFloorPref.setTitle("Active floor info");
        useFloorPref.setSummary("Use floor number");
	    dialogBasedPrefCat.addPreference(useFloorPref);
	    
        // actual floor
        String floor = sharedPreferences.getString(KEY_FLOOR, null);
		if (floor == null)
			floor = "0";
		
		boolean is_enabled = sharedPreferences.getBoolean(KEY_USE_FLOOR, false);
        
		EditTextPreference floorTestPref = new EditTextPreference(this);
		floorTestPref.setEnabled(is_enabled);
        floorTestPref.setDialogTitle("Number of Floor");
        floorTestPref.setKey(KEY_FLOOR);
        floorTestPref.setTitle("Floor");
        floorTestPref.setSummary(floor);
        dialogBasedPrefCat.addPreference(floorTestPref);
        
        /* Height Threshold*/
	    PreferenceCategory dialogBasedPrefCat2 = new PreferenceCategory(this);
	    dialogBasedPrefCat2.setTitle("Altitude Threshold");
	    root.addPreference(dialogBasedPrefCat2);
	    
	    CheckBoxPreference useThresholdPref = new CheckBoxPreference(this);
	    useThresholdPref.setKey(KEY_IS_DIST_FILTER);
	    useThresholdPref.setTitle("Active altitude threshold");
	    useThresholdPref.setSummary("Use threshold to see altitude diffs");
	    dialogBasedPrefCat2.addPreference(useThresholdPref);
	    
        // threshold
        String thrhld = sharedPreferences.getString(KEY_DIST_FILTER, null);
		if (thrhld == null)
			thrhld = "0";
		
		is_enabled = sharedPreferences.getBoolean(KEY_IS_DIST_FILTER, false);
        
		EditTextPreference thresholdTestPref = new EditTextPreference(this);
		thresholdTestPref.setEnabled(is_enabled);
		thresholdTestPref.setDialogTitle("Threshold (meters)");
		thresholdTestPref.setKey(KEY_DIST_FILTER);
		thresholdTestPref.setTitle("Threshold");
		thresholdTestPref.setSummary(thrhld);
        dialogBasedPrefCat2.addPreference(thresholdTestPref);
        
//        /* Movement line */
//	    PreferenceCategory dialogBasedPrefCat3 = new PreferenceCategory(this);
//	    dialogBasedPrefCat3.setTitle("Resources movement");
//	    root.addPreference(dialogBasedPrefCat3);
//	    
//	    // 
//	    CheckBoxPreference movementPref = new CheckBoxPreference(this);
//	    movementPref.setKey(KEY_MOVEMENT_LINE);
//	    movementPref.setTitle("Active movement line");
//	    movementPref.setSummary("Show resources movement line on screen");
//	    dialogBasedPrefCat3.addPreference(movementPref);
	    
	    /* Show measure data */
	    PreferenceCategory dialogBasedPrefCat4 = new PreferenceCategory(this);
	    dialogBasedPrefCat4.setTitle("Measures info");
	    root.addPreference(dialogBasedPrefCat4);
	    
	    // 
	    CheckBoxPreference measuresPref = new CheckBoxPreference(this);
	    measuresPref.setKey(KEY_MEASURES);
	    measuresPref.setTitle("Active measures showing");
	    measuresPref.setSummary("Show measures info on screen");
	    dialogBasedPrefCat4.addPreference(measuresPref);
	    
	    /* Show objects labels */
	    PreferenceCategory dialogBasedPrefCat5 = new PreferenceCategory(this);
	    dialogBasedPrefCat5.setTitle("Labels");
	    root.addPreference(dialogBasedPrefCat5);
	    
	    // 
//	    CheckBoxPreference labelsPref = new CheckBoxPreference(this);
//	    labelsPref.setKey(KEY_LABELS);
//	    labelsPref.setTitle("Active labels");
//	    labelsPref.setSummary("Show the labels of the objects");
//	    dialogBasedPrefCat5.addPreference(labelsPref);
	    
	    // 
	    CheckBoxPreference moveLabelsPref = new CheckBoxPreference(this);
	    moveLabelsPref.setKey(KEY_MOVE_LABELS);
	    moveLabelsPref.setTitle("Move summary");
	    moveLabelsPref.setSummary("Move the summary boxes");
	    dialogBasedPrefCat5.addPreference(moveLabelsPref);
	    
//	    /* Compass correction */
//	    PreferenceCategory dialogBasedPrefCat6 = new PreferenceCategory(this);
//	    dialogBasedPrefCat6.setTitle("Compass correction");
//	    root.addPreference(dialogBasedPrefCat6);
//	    
//	    String correction = sharedPreferences.getString(KEY_COMPASS_CORRECTION, null); 
//		 
//		if (correction == null)
//			correction = "0.1";
//			     
//		EditTextPreference userHeightTestPref6 = new EditTextPreference(this);
//        userHeightTestPref6.setDialogTitle("Compass correction (º)");
//        userHeightTestPref6.setKey(KEY_COMPASS_CORRECTION);
//        userHeightTestPref6.setTitle("Correction");
//        userHeightTestPref6.setSummary(correction);
//        userHeightTestPref6.setEnabled(false);
//        dialogBasedPrefCat6.addPreference(userHeightTestPref6);
//        
//        // 
//	    CheckBoxPreference signumCorrectionPref = new CheckBoxPreference(this);
//	    signumCorrectionPref.setKey(KEY_CORRECTION_SIGNUM);
//	    signumCorrectionPref.setTitle("Signum");
//	    signumCorrectionPref.setSummary("Compass correction negative signum");
//	    dialogBasedPrefCat6.addPreference(signumCorrectionPref);
        
	    return root;
	        
	}

	public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) 
	{
		if (key.equals(KEY_NOT_HEIGHT)){
			blockVisibility();
			
		}else if (key.equals(KEY_HEIGHT)) {

			Preference pref = this.findPreference(key);
			if(pref == null) 
				return;
			String height = sharedPreferences.getString(key, "");
			if(height.contains(",")){
				height = height.replace(",", ".");

				Editor editor = sharedPreferences.edit();
				editor.putString(KEY_HEIGHT, height);
				editor.commit();
			}
			
			pref.setSummary(height);
		}else if (key.equals(KEY_USE_FLOOR)) {

			Preference pref = this.findPreference(KEY_FLOOR);
			if(pref == null) 
				return;
			
			pref.setEnabled(sharedPreferences.getBoolean(key, false));
			
		}else if (key.equals(KEY_FLOOR)) {

			Preference pref = this.findPreference(key);
			if(pref == null) 
				return;
			
			pref.setSummary(sharedPreferences.getString(key, ""));
		}else if (key.equals(KEY_IS_DIST_FILTER)) {

			Preference pref = this.findPreference(KEY_DIST_FILTER);
			if(pref == null) 
				return;
			
			pref.setEnabled(sharedPreferences.getBoolean(key, false));
		}else if(key.equals(KEY_DIST_FILTER)) {

			Preference pref = this.findPreference(key);
			if(pref == null) 
				return;
			
			pref.setSummary(sharedPreferences.getString(key, ""));
		}
//		}else if(key.equals(KEY_CORRECTION_SIGNUM)) {
//			
//			boolean negative = sharedPreferences.getBoolean(key, false);
//			
//			String value = sharedPreferences.getString(KEY_COMPASS_CORRECTION, "0.1");
//			if(negative){
//				if(!value.contains("-"))
//					value = "-" + value;
//			}else
//				if(value.contains("-"))
//					value = value.replace("-", "");
//			Editor editor = sharedPreferences.edit();
//			editor.putString(KEY_COMPASS_CORRECTION, value);
//			editor.commit();
//			
//			Preference pref = this.findPreference(KEY_COMPASS_CORRECTION);
//			pref.setSummary(value);
//		}
	    
	}
	
	private void blockVisibility(){
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

		Preference pref = this.findPreference(KEY_NOT_HEIGHT);
		if(pref == null) 
			return;

		boolean not_visible = sharedPreferences.getBoolean(KEY_NOT_HEIGHT, false);

		pref = this.findPreference(KEY_GPS);
		pref.setEnabled(!not_visible);

		pref = this.findPreference(KEY_HEIGHT);
		pref.setEnabled(!not_visible);

		pref = this.findPreference(KEY_USE_FLOOR);
		pref.setEnabled(!not_visible);

		pref = this.findPreference(KEY_FLOOR);
		if(!not_visible && sharedPreferences.getBoolean(KEY_USE_FLOOR, false))
			pref.setEnabled(true);
		else
			pref.setEnabled(false);

		pref = this.findPreference(KEY_IS_DIST_FILTER);
		pref.setEnabled(!not_visible);

		pref = this.findPreference(KEY_DIST_FILTER);
		if(!not_visible && sharedPreferences.getBoolean(KEY_IS_DIST_FILTER, false))
			pref.setEnabled(true);
		else
			pref.setEnabled(false);
	}
	
}